package com.example.shootinggamebackend.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.ContentCachingRequestWrapper;


import java.io.File;
import java.io.IOException;

import java.util.Enumeration;


@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileUploadController {

    // 设置上传文件的存储路径
    private static final String UPLOAD_DIR = "/home/faun/Videos";


    @PostMapping("/upload")
    public String handleFileUpload(
            @RequestParam("file") MultipartFile file
    ) {
        try {
            // 打印文件信息
            System.out.println("Original File Name: " + file.getOriginalFilename());
            System.out.println("File Size: " + file.getSize());
            System.out.println("Content Type: " + file.getContentType());

            // 保存文件到服务器
            String filePath = UPLOAD_DIR + "/" + file.getOriginalFilename();
            file.transferTo(new File(filePath));

            return "文件上传成功：" + filePath;
        } catch (IOException e) {
            e.printStackTrace();
            return "文件上传失败";
        }
    }
//    public String upload(MultipartHttpServletRequest request){
////        System.out.println("文件上传数量： "+multipartFiles.length);
//        try {
//            // 打印请求的基本信息
//            System.out.println("Request Method: " + request.getMethod());
//            System.out.println("Request URI: " + request.getRequestURI());
//            System.out.println("Query String: " + request.getQueryString());
//
//            // 打印请求头信息
//            System.out.println("Request Headers:");
//            Enumeration<String> headerNames = request.getHeaderNames();
//            while (headerNames.hasMoreElements()) {
//                String headerName = headerNames.nextElement();
//                System.out.println(headerName + ": " + request.getHeader(headerName));
//            }
//
//            System.out.println("打印文件...");
//            System.out.println(request.getMultiFileMap().size());
//            System.out.println(request.getMultiFileMap().get("file"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "上传失败!";
//    }
}
